package other.barcode;

import other.database.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author bingo
 * @Description
 * @Date 2018/8/6
 */
public class BarcodeSaveToDatabase implements  BarcodeSaveService {

    private Connection connection;
    @Override
    public void save(String barcode) {
        PreparedStatement preparedStatement  =null;
        String sql = "insert into barcode (barcode) values(?)";
        connection = DBUtil.getConnection();
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,barcode);
            preparedStatement.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void finish() {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
